document.addEventListener('DOMContentLoaded', function() {
    console.log('load-navbar.js loaded');
    
    // 创建导航栏容器
    const navbarContainer = document.createElement('div');
    navbarContainer.id = 'navbar-container';
    document.body.insertBefore(navbarContainer, document.body.firstChild);

    // 加载导航栏HTML
    fetch('menu.html')
        .then(response => response.text())
        .then(data => {
            // 移除script标签以避免重复加载
            const tempDiv = document.createElement('div');
            tempDiv.innerHTML = data;
            const scripts = tempDiv.querySelectorAll('script');
            scripts.forEach(script => script.remove());
            
            navbarContainer.innerHTML = tempDiv.innerHTML;
            
            // 创建并加载必要的脚本
            loadRequiredScripts();
        })
        .catch(error => {
            console.error('加载导航栏失败:', error);
            navbarContainer.innerHTML = '<p>导航栏加载失败</p>';
        });

    // 加载必要的脚本
    function loadRequiredScripts() {
        const scripts = [
            '/admin/js/axios.js',
            '/admin/js/config.js',
            '/admin/js/menu-loader.js',
            '/admin/js/script.js',
            '/admin/js/user-info.js' // 确保加载用户信息脚本
        ];
        
        let loadedScripts = 0;
        
        scripts.forEach(src => {
            const script = document.createElement('script');
            script.src = src;
            script.onload = () => {
                loadedScripts++;
                if (loadedScripts === scripts.length) {
                    // 所有脚本加载完成后，初始化菜单
                    setTimeout(() => {
                        if (typeof initNavbar !== 'undefined') {
                            initNavbar();
                        }
                        if (typeof highlightCurrentPage !== 'undefined') {
                            highlightCurrentPage();
                        }
                        // 确保loadMenu函数被调用
                        if (typeof loadMenu !== 'undefined') {
                            loadMenu();
                        }
                        // 确保用户信息被加载
                        if (typeof loadUserInfo !== 'undefined') {
                            console.log('Calling loadUserInfo from load-navbar.js');
                            loadUserInfo();
                        } else if (typeof initUserInfo !== 'undefined') {
                            console.log('Calling initUserInfo from load-navbar.js');
                            initUserInfo();
                        }
                    }, 100);
                }
            };
            script.onerror = () => {
                console.error('加载脚本失败:', src);
            };
            document.body.appendChild(script);
        });
    }

    // 初始化导航栏功能
    function initNavbar() {
        const menuItems = document.querySelectorAll('.menu-item.has-submenu');

        menuItems.forEach(item => {
            const link = item.querySelector('a');

            link.addEventListener('click', function(e) {
                e.preventDefault();

                // 关闭其他展开的菜单项
                menuItems.forEach(otherItem => {
                    if (otherItem !== item) {
                        otherItem.classList.remove('open');
                    }
                });

                // 切换当前菜单项的展开状态
                item.classList.toggle('open');
            });
        });
    }

    // 导出给其他文件使用
    window.initNavbar = initNavbar;

    // 高亮当前页面并展开对应菜单
    function highlightCurrentPage() {
        const currentPage = window.location.pathname.split('/').pop();
        const navLinks = document.querySelectorAll('.nav-link');

        navLinks.forEach(link => {
            const linkPage = link.getAttribute('href');
            if (linkPage === currentPage) {
                // 高亮当前链接
                link.classList.add('active');
                
                // 展开父菜单
                const parentMenu = link.closest('.menu-item.has-submenu');
                if (parentMenu) {
                    parentMenu.classList.add('open');
                }
            }
        });
    }

    // 导出给其他文件使用
    window.highlightCurrentPage = highlightCurrentPage;
});
